﻿using ReactiveUI;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using OpenCvSharp.WpfExtensions;

namespace ScienceAIApp.Views;
/// <summary>
/// Interaction logic for ImageMarkDrawView.xaml
/// </summary>
public partial class ImageMarkDrawView
{
    public ImageMarkDrawView()
    {
        InitializeComponent();
        this.WhenActivated(_activated);
    }

    [Obsolete("Obsolete")]
    private void _activated(CompositeDisposable disposable)
    {
        this.OneWayBind(
                ViewModel,
                mode => mode.Count,
                view => view.ViewScrollBar.Maximum,
                count => count - 1)
            .DisposeWith(disposable);

        this.OneWayBind(
                ViewModel,
                mode => mode.CurrentIndex,
                view => view.ViewScrollBar.Value)
            .DisposeWith(disposable);


        this.OneWayBind(
                ViewModel,
                mode => mode.CurrentIndex,
                view => view.IndexBlock.Text)
            .DisposeWith(disposable);


        this.OneWayBind(
                ViewModel,
                mode => mode.Count,
                view => view.CountBlock.Text)
            .DisposeWith(disposable);


        this.ViewScrollBar
            .Events()
            .ValueChanged
            //.Throttle(TimeSpan.FromMilliseconds(150))
            .Select(arg => (int)arg.NewValue)
            .InvokeCommand(this, view => view.ViewModel!.GotoIndexCommand)
            .DisposeWith(disposable);

        // 靶点标记
        // 布针按钮
        this.TransformGrid.Events()
            .MouseLeftButtonDown
            .Select(e => e.GetPosition(this.ImageRec))
            .InvokeCommand(this, v => v.ViewModel!.ImageMouseDownCommand)
            .DisposeWith(disposable);

        // 移动滚动条
        this.MouseWheelGridPanel
            .Events()
            .MouseWheel
            .Select(arg => (int)arg.Delta)
            .ObserveOnDispatcher()
            .InvokeCommand(this, view => view.ViewModel!.MouseWheelUpdateCurrentIndexCommand)
            .DisposeWith(disposable);

        #region 大小图切换
        this.OneWayBind(
            ViewModel,
            vm=>vm.IsZoomOut,
            v=>v.EnlargeImg.Visibility,
            isZoomOut=>isZoomOut ? Visibility.Hidden : Visibility.Visible).DisposeWith(disposable);

        this.OneWayBind(
            ViewModel,
            vm => vm.IsZoomOut,
            v => v.NarrowImg.Visibility,
            isZoomOut => isZoomOut ? Visibility.Visible : Visibility.Hidden).DisposeWith(disposable);

        this.BindCommand(
            ViewModel,
            vm=>vm.ZoomInOutCommand,
            v=>v.NarrowImg,
            Observable.Return(false),
            nameof(this.NarrowImg.MouseDown)
            ).DisposeWith(disposable);

        this.BindCommand(
            ViewModel,
            vm => vm.ZoomInOutCommand,
            v => v.EnlargeImg,
            Observable.Return(true),
            nameof(this.EnlargeImg.MouseDown)
        ).DisposeWith(disposable);
        #endregion

        #region 图
        this.OneWayBind(
                ViewModel,
                model => model.OriginMat,
                view => view.OriginImage.Source,
                image =>
                {
                    var result = image?.ToWriteableBitmap();
                    return result;
                })
            .DisposeWith(disposable);

        this.OneWayBind(
                ViewModel,
                model => model.LableMat,
                view => view.LableMat.Source,
                image =>
                {
                    var result = image?.ToWriteableBitmap();
                    return result;
                })
            .DisposeWith(disposable);

        //this.OneWayBind(
        //        ViewModel,
        //        model => model.TfccMat,
        //        view => view.TfccMat.Source,
        //        image =>
        //        {
        //            var result = image?.ToWriteableBitmap();
        //            return result;
        //        })
        //    .DisposeWith(disposable);

        //this.OneWayBind(
        //        ViewModel,
        //        model => model.UlnaMat,
        //        view => view.UlnaMat.Source,
        //        image =>
        //        {
        //            var result = image?.ToWriteableBitmap();
        //            return result;
        //        })
        //    .DisposeWith(disposable);

        this.OneWayBind(
                ViewModel,
                model => model.MarkMat,
                view => view.MarkImage.Source,
                image =>
                {
                    var result = image?.ToWriteableBitmap();
                    return result;
                })
            .DisposeWith(disposable);
        
        // 十字线
        this.OneWayBind(
                ViewModel,
                model => model.CrossLineMat,
                view => view.CrossLineMat.Source,
                image =>
                {
                    var result = image?.ToWriteableBitmap();
                    return result;
                })
            .DisposeWith(disposable);

        // Tag
        this.OneWayBind(
                ViewModel,
                model => model.TagMat,
                view => view.TagInfoImage.Source,
                image =>
                {
                    var result = image?.ToWriteableBitmap();
                    return result;
                })
            .DisposeWith(disposable);

        #endregion

        #region 十字线
        this.TransformGrid.Events()
                .MouseMove
                .Select(e =>
                {
                    if (e.LeftButton == MouseButtonState.Pressed)
                        return e.GetPosition(this.ImageRec);
                    return new Point(-1, -1);
                })
                .InvokeCommand(this, v => v.ViewModel!.ImageLeftDownMoveCommand)
                .DisposeWith(disposable);
        #endregion
    }
}
